package com.yupi.ojbackend.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.ojbackend.common.BaseResponse;
import com.yupi.ojbackend.common.ErrorCode;
import com.yupi.ojbackend.common.ResultUtils;
import com.yupi.ojbackend.exception.BusinessException;
import com.yupi.ojbackend.judge.JudgeService;
import com.yupi.ojbackend.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.yupi.ojbackend.model.dto.questionsubmit.QuestionSumbitAddRequest;
import com.yupi.ojbackend.model.entity.QuestionSubmit;
import com.yupi.ojbackend.model.entity.User;
import com.yupi.ojbackend.model.vo.QuestionSubmitVO;
import com.yupi.ojbackend.service.QuestionSubmitService;
import com.yupi.ojbackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * 題目提交接口
 *
 * @author yyt
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    @Resource
    private JudgeService judgeService;

//    /**
//     * 题目提交
//     *
//     * @param questionSumbitAddRequest
//     * @param request
//     * @return questionSubmitId 本次提交id
//     */
//    @PostMapping("/")
//    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSumbitAddRequest questionSumbitAddRequest,
//                                               HttpServletRequest request) {
//        if (questionSumbitAddRequest == null || questionSumbitAddRequest.getQuestionId() <= 0) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        // 登录才能点赞
//        final User loginUser = userService.getLoginUser(request);
//        long questionId = questionSumbitAddRequest.getQuestionId();
//        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSumbitAddRequest, loginUser);
////        调用判题服务
//        CompletableFuture.runAsync(()->{
//            judgeService.doJudge(questionSubmitId);
//        });
//        return ResultUtils.successLong(questionSubmitId);
//    }
//
//    /**
//     * 分页获取题目提交列表（ps：仅本人和管理员能看见提交代码）
//     *
//     * @param questionSubmitQueryRequest
//     * @param request
//     * @return
//     */
//    @PostMapping("/list/page/vo")
//    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitVOByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
//                                                                           HttpServletRequest request) {
//        long current = questionSubmitQueryRequest.getCurrent();
//        long size = questionSubmitQueryRequest.getPageSize();
////        从数据库中获取到原始的questionSubmit
//        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
//                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
////        返回脱敏信息
//        User loginUser = userService.getLoginUser(request);
//        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUser));
//    }
}
